package com.itheima.reggie_test.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie_test.common.Result;
import com.itheima.reggie_test.dao.CategoryDao;
import com.itheima.reggie_test.entity.Category;
import com.itheima.reggie_test.entity.Dish;
import com.itheima.reggie_test.entity.Setmeal;
import com.itheima.reggie_test.service.ICategoryService;
import com.itheima.reggie_test.service.IDishService;
import com.itheima.reggie_test.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ auther: 小熊
 * @ date:2022/12/15 16:31
 * @Todo :
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements ICategoryService {
    @Autowired
    private IDishService dishService;
    @Autowired
    private ISetmealService setmealService;
    @Autowired
    private CategoryDao categoryDao;
    @Override
    public Result removeFromId(Long id) {
        //dish
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId,id);
        int dishCount = dishService.count(dishWrapper);
        if (dishCount>0){
            return Result.error("当前分类下有菜品，不能删除");
        }
        //setmeal
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId,id);
        int setmealCount = setmealService.count(setmealWrapper);
        if (setmealCount>0){
            return Result.error("当前分类下有套餐，不能删除");
        }
        categoryDao.deleteById(id);
        return Result.success("删除成功");
    }
}
